<template>
  <div class="like-container">
    <van-icon class="liked"
    color="#888"
    :name="value ===1 ? 'good-job':'good-job-o'"
    :loading="loading"
    @click="onlike"
  />
  </div>
</template>

<script>
import { addLike, deleteLike } from '@/api/article'
export default {
  name: 'likeArticle',
  props: {
    value: {
      type: Number,
      require: true
    },
    articleId: {
      type: [Number, String, Object],
      require: true
    }

  },
  data () {
    return {
      loading: false
    }
  },

  mounted () {

  },

  methods: {
    async onlike () {
      this.loading = true // 点赞按钮的loading

      try {
        let status = -1
        if (this.value === 1) {
          // 已点赞 取消
          await deleteLike(this.articleId)
          status = 0
        } else {
          // 没有点赞 点击点赞
          await addLike(this.articleId)
          status = 1
        }
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast.fail('操作失败，请重试！')
      }
      this.loading = false
      // this.followLoading = false
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
